package com.demo.mq.rocket.consumer.simple;

import org.apache.rocketmq.client.consumer.DefaultLitePullConsumer;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.impl.consumer.DefaultLitePullConsumerImpl;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

import static com.demo.mq.rocket.consumer.Const.ROCKET_DEFAULT_GROUP;
import static com.demo.mq.rocket.consumer.Const.ROCKET_NAMESRVADDR;

/**
 * 消费者自己从服务器端拉取消息
 */
public class SimplePullConsumer {

    public static void main(String[] args) throws InterruptedException, MQClientException {

        // 指定组
        DefaultLitePullConsumer consumer = new DefaultLitePullConsumer(ROCKET_DEFAULT_GROUP);
        // 指定NameService  的地址,多个使用 ; 隔开
        consumer.setNamesrvAddr(ROCKET_NAMESRVADDR);
        // 指定topic
        consumer.subscribe("simple_topic", "*");
        consumer.start();
        // 消费消息
        while (true) {
            List<MessageExt> messageExts = consumer.poll(1000);
            System.out.println(messageExts.size());
            for (MessageExt msg : messageExts) {
                System.out.println(new String(msg.getBody()));
            }
        }
    }

}
